<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>产品报检(工位用)</title>
<meta name="renderer" content="webkit">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<link rel="stylesheet" href="${pageContext.request.contextPath }/assets/css/layui.css"  media="all">
<link rel="stylesheet" href="${pageContext.request.contextPath }/assets/css/toastr.min.css"  media="all">
</head>
<body>
<script type="text/html" id="toolbarDemo">
	<div class="layui-btn-container">
		<button class="layui-btn layui-btn-blue" lay-event="binding"><i class="layui-icon layui-icon-add-1">  台车绑定</i></button>
		<button class="layui-btn layui-btn-blue" lay-event="back"><i class="layui-icon layui-icon-return">  返回</i></button>
	</div>
</script>

<div id="DivID" hidden="hidden">
	<form class="layui-form layui-card-body" id="addFormId">
		<table>
			<tr>
				<td>
					<label class="layui-form-label">台车编码 <font style="color:red; font-size: 24px;">*</font></label>
					<input class="layui-input" id="trolleyNumber" lay-verify="trolleyNumber" name="trolleyNumber" type="text" value="" style="width:300px;"  onblur="selecTrolley()">
					<label class="layui-form-label">产品条码 <font style="color:red; font-size: 24px;">*</font></label>
					<textarea placeholder="请扫描产品条码" lay-verify="piCode" id="piCode" name="piCode" style="width:300px;height: 250px" class="layui-textarea"></textarea>
				</td>
				<td>
					<label class="layui-form-label">产品编码</label>
					<input class="layui-input" lay-verify="remark01" id="remark01" name="remark01" type="text" value="" style="width:300px;">
					<label class="layui-form-label">产品名称</label><input class="layui-input" lay-verify="piName" id="piName" name="piName" type="text" value="" style="width:300px;">
					<label class="layui-form-label">产品规格</label><input class="layui-input" lay-verify="piStandard" id="piStandard" name="piStandard" type="text" value="" style="width:300px;">
					<label class="layui-form-label">计量单位</label><input class="layui-input" lay-verify="piUnit" id="piUnit" name="piUnit" type="text" value="" style="width:300px;">
					<label class="layui-form-label">数量</label><input class="layui-input" lay-verify="piNum" id="piNum" name="piNum" type="number" value="" style="width:300px;">
					<input class="layui-hide" lay-verify="tStatus" name="tStatus" value="0">
				</td>
			</tr>
			<tr>
				<td  colspan="2" align="center">
					</br></br>
					<button class="layui-btn layui-btn-blue" id="savebuding" lay-submit lay-filter="addform">绑定</button>
					<button type="reset" id="addReset" class="layui-btn layui-btn-primary" style="margin-left:140px">重置</button>
				</td>
			</tr>
		</table>
	</form>
</div>

<div id="DivID2" hidden="hidden">
	<form class="layui-form layui-card-body" id="addFormId">
		<table>
			<tr>
				<td>
					<label class="layui-form-label">台车编码 <font style="color:red; font-size: 24px;">*</font></label>
					<input class="layui-input" id="trolleyNumber" lay-verify="trolleyNumber" name="trolleyNumber" type="text" value="" style="width:300px;"  onblur="selecTrolley()">
					<label class="layui-form-label">产品条码 <font style="color:red; font-size: 24px;">*</font></label>
					<textarea placeholder="请扫描产品条码" lay-verify="piCode" id="piCode" name="piCode" style="width:300px;height: 250px" class="layui-textarea"></textarea>
				</td>
				<td>
					<label class="layui-form-label">产品编码</label>
					<input class="layui-input" lay-verify="remark01" id="remark01" name="remark01" type="text" value="" style="width:300px;">
					<label class="layui-form-label">产品名称</label><input class="layui-input" lay-verify="piName" id="piName" name="piName" type="text" value="" style="width:300px;">
					<label class="layui-form-label">产品规格</label><input class="layui-input" lay-verify="piStandard" id="piStandard" name="piStandard" type="text" value="" style="width:300px;">
					<label class="layui-form-label">计量单位</label><input class="layui-input" lay-verify="piUnit" id="piUnit" name="piUnit" type="text" value="" style="width:300px;">
					<label class="layui-form-label">数量</label><input class="layui-input" lay-verify="piNum" id="piNum" name="piNum" type="number" value="" style="width:300px;">
					<input class="layui-hide" lay-verify="tStatus" name="tStatus" value="0">
				</td>
			</tr>
			<tr>
				<td colspan="2" align="center">
					</br></br>
					<button class="layui-btn layui-btn-blue" lay-submit lay-filter="addform">绑定</button>
					<button type="reset" id="addReset" class="layui-btn layui-btn-primary" style="margin-left:140px">重置</button>
				</td>
			</tr>
		</table>
	</form>
</div>

<table class="layui-hide" id="bindingList" lay-filter="bindingList"></table>
<script type="text/html" id="barDemo">
	<a class="layui-btn layui-btn-xs layui-btn-warm" lay-event="unbind">解绑</a>
	<a class="layui-btn layui-btn-xs" lay-event="rebind">修改绑定</a>
	<a class="layui-btn layui-btn-xs" lay-event="inspect" id="inspect">报检</a>
</script>
<script src="${pageContext.request.contextPath }/assets/layui.js" charset="utf-8"></script>
<script src="${pageContext.request.contextPath }/assets/layui.all.js"></script>
<script src="${pageContext.request.contextPath }/assets/js/jquery.min.js" charset="utf-8"></script>
<script src="${pageContext.request.contextPath }/assets/js/toastr.min.js" charset="utf-8"></script>

<script type="text/javascript">

	function selecTrolley() {
		//获取台车条码
   		var trolleyNumber = $("#trolleyNumber").val();
   	 $.ajax({
	    	type:'post',
	    	dataType: "json" ,
	    	url:'${pageContext.request.contextPath}/cut/selecTrolley.do',
	    	data:{"trolleyNumber":trolleyNumber},
	    	success:function(data){
	    		if(data >= 1) {
	    			toastr.warning("对不起，此台车正在使用！");
	    			$("#trolleyNumber").focus(); 
	    	        $("#trolleyNumber").select();
	    		}
	    	}
	    });
	}
	
	
	
	function selecPro() {
		
		//获取产品条码
		var piCode = $("#piCode").val();
		if($("#piCode").val() != null && $("#piCode").val() != "" && $("#piCode").val() != undefined) {
		
			if(piCode.length < parseInt(16)) {
				toastr.warning("对不起，产品条码不符合规则！请重新扫描");
			}else {
				var pnum = piCode.substring(3,7)
				 $.ajax({
			    	type:'post',
			    	dataType: "json" ,
			    	url:'${pageContext.request.contextPath }/cut/selsectPro.do',
			    	data:{"pnum":pnum},
			    	success:function(data){
			    		//产品编码
			    		$("#remark01").val(data[0].pnum);
			    		//产品名称
			    		$("#piName").val(data[0].pname);
			    		//产品规格
			    		$("#piStandard").val(data[0].pcar);
			    		//计量单位
			    		$("#piUnit").val(data[0].pqf);
			    	}
			    })
			}
		}
	}
	
</script>

<script>

var x = 0;

function keyDown(e){  
    //IE内核浏览器  
     if (navigator.appName == 'Microsoft Internet Explorer'){  
           var keycode = event.keyCode;  
           var realkey = String.fromCharCode(event.keyCode);  
     }else {//非IE内核浏览器  
           var keycode = e.which;  
           var realkey = String.fromCharCode(e.which);  
     }  

   //监听Ctrl键  
   if(keycode==13){  
	   
	   if($("#piCode").val() != null && $("#piCode").val() != "" && $("#piCode").val() != undefined) {
		  var piCode =  $("#piCode").val();
		  var arrCount =  piCode.split(",");
		  var mo = arrCount[arrCount.length - 1];
		  if(mo != null && mo != "" && mo != undefined) {
		  	x = arrCount.length;
		  }else {
		  	x = arrCount.length - 1;
		  }
		  $("#piNum").val(x);
		  var str = "";
		  var str2 = "";
		  var index = piCode .lastIndexOf(",");
		  //最后一个条码
		  str  = piCode .substring(index + 1, piCode .length);
		  str2  = piCode .substring(0,index + 1);
		 var arr =  piCode.split(",");
		 if(x == 1) {
			  selecPro();
		  }
		 if(x > 1) {
			  for(var i = 0;i < arr.length-1;i++) {
				 if(str == arr[i] ){
					toastr.warning("对不起，条码已存在，请重新扫码！");
	    			$("#piCode").val(str2);
	    			if(x != 0) {
	    				x = x-1;
	    				$("#piNum").val(x);
	    			}
	    			return;
				}
			 }
		  }
		 
		 if(str != null && str != "" && str != undefined) {
			 $.ajax({
			    	type:'post',
			    	dataType: "json" ,
			    	url:'${pageContext.request.contextPath }/cut/checkTube.do',
			    	data:{"ctube":str},
			    	success:function(data){
			    		if(data == 0) {
			    			toastr.warning("对不起，产品还未切割，不能进行报检");
			    			$("#piCode").val(str2);
			    			if(x != 0) {
			    				x = x-1;
			    				$("#piNum").val(x);
			    			}
			    		}else if(data == -1) {
			    			toastr.warning("对不起，产品已绑定台车！");
			    			$("#piCode").val(str2);
			    			if(x != 0) {
			    				x = x-1;
			    				$("#piNum").val(x);
			    			}
			    		}else if(data == -2) {
			    			toastr.warning("对不起，产品已报检！");
			    			$("#piCode").val(str2);
			    			if(x != 0) {
			    				x = x-1;
			    				$("#piNum").val(x);
			    			}
			    		}else {
			    			   //判断所有产品是否是同一种产品
		    			  if(x>1) {
		    				  var p1 =  str2.substring(3, 7);
			    			  var p2 =  str.substring(3, 7);
			    			  if(p1 == p2) {
			    			 	$("#piCode").val(piCode+",");
			    			  }else {
			    				  toastr.warning("对不起，请绑定同一种产品");
				    			  str2  = piCode .substring(0,index + 1);
				    			  $("#piCode").val(str2);
				    			  if(x != 0) {
					    				x = x-1;
					    				$("#piNum").val(x);
					    			}
			    			  }
			    			}else if(x == 1){
			    				$("#piCode").val(piCode+",");
			    			}
			    		}
			    	}
			    });
		 }
		  
		   return;
	   }else if($("#trolleyNumber").val() != null && $("#trolleyNumber").val() != "" && $("#trolleyNumber").val() != undefined) {
		   	$("#piCode").focus(); 
	        $("#piCode").select();
	   }
	}  
}
//按键事件
document.onkeydown=keyDown;


layui.use(['table','layer','upload','form'], function(){
	var table = layui.table;
	var layer = layui.layer,
    form = layui.form;
	
	table.render({
	    elem: '#bindingList'
	    ,url:'${pageContext.request.contextPath }/productInspection/selectTrolleyBindingList.do'
	    ,toolbar: '#toolbarDemo'
	    ,title: '绑定列表'
	    ,limits:[10,20,30]
	    ,cols: [[
		  {field:'trolleyNumber', title:'台车编码'}
	      ,{field:'piCode', title:'产品条码'}
	      ,{field:'remark01', title:'产品编码'}
	      ,{field:'piName', title:'产品名称'}
	      ,{field:'piStandard', title:'产品规格'}
	      ,{field:'piUnit', title:'计量单位'}
	      ,{field:'piNum', title:'数量'}
	      ,{fixed: 'right', title:'操作', toolbar: '#barDemo'}
	    ]]
	    ,page: true
	});
  
	//头工具栏事件
	table.on('toolbar(bindingList)', function(obj){
		var checkStatus = table.checkStatus(obj.config.id);
		var content = 	'<form class="layui-form layui-card-body" id="addFormId">'+
							'<div class="layui-form-item">'+
								'<div class="layui-inline">'+
									'<label class="layui-form-label">台车编码</label>'+
									'<input class="layui-input" id="trolleyNumber" lay-verify="trolleyNumber" name="trolleyNumber" type="text" value="" style="width:300px;">'+
								'</div>'+
								'<div class="layui-inline">'+
									'<font style="color:red; font-size: 24px;">*</font>'+
							    '</div>'+
							'</div>'+
							
							'<div class="layui-form-item">'+
								'<div class="layui-inline">'+
									'<label class="layui-form-label">产品条码</label>'+
									/* '<input class="layui-input" lay-verify="piCode" id="piCode" name="piCode" type="text" value="" style="width:300px;" onblur="selecPro()">'+ */
									'<textarea placeholder="请扫描产品条码" lay-verify="piCode" id="piCode" name="piCode" style="width:300px;height: 250px" class="layui-textarea"></textarea>'+
									'</div>'+
								'<div class="layui-inline">'+
									'<font style="color:red; font-size: 24px;">*</font>'+
							    '</div>'+
							'</div>'+
							
							'<div class="layui-form-item">'+
								'<label class="layui-form-label">产品编码</label>'+
								'<input class="layui-input" lay-verify="remark01" id="remark01" name="remark01" type="text" value="" style="width:300px;">'+
							'</div>'+
							'<div class="layui-form-item"><input class="layui-input" lay-verify="piName" id="piName" name="piName" type="text" value="" style="width:300px;"></div>'+
							'<div class="layui-form-item"><label class="layui-form-label">产品规格</label><input class="layui-input" lay-verify="piStandard" id="piStandard" name="piStandard" type="text" value="" style="width:300px;"></div>'+
							'<div class="layui-form-item"><label class="layui-form-label">计量单位</label><input class="layui-input" lay-verify="piUnit" id="piUnit" name="piUnit" type="text" value="" style="width:300px;"></div>'+
							'<div class="layui-form-item"><label class="layui-form-label">数量</label><input class="layui-input" lay-verify="piNum" id="piNum" name="piNum" type="number" value="" style="width:300px;"></div>'+
							'<input class="layui-hide" lay-verify="tStatus" name="tStatus" value="0">'+
						  		'<div class="layui-form-item"><div class="layui-input-block">'+
									'<button class="layui-btn layui-btn-blue" lay-submit lay-filter="addform">绑定</button><button type="reset" id="addReset" class="layui-btn layui-btn-primary" style="margin-left:140px">重置</button>'+
								'</div></div>'+
							'</form>';
	    switch(obj.event){
	    	case 'binding':
	    		layer.open({
					  type: 1 		//Page层类型
					  ,area: ['780px', '550px'] //宽  高
					  ,title: '台车绑定'
					  ,shade: 0.1 	//遮罩透明度
					  ,maxmin: true //允许全屏最小化
					  ,anim: 1 		//0-6的动画形式，-1不开启
					  //,content: content
					  ,content: $("#DivID")
					  ,end: function(){
				  		  $("#DivID").hide();
				  	  }
					  ,success:function(layero, index){
						  $("#addReset").click();
						  form.render();
						  
						  $("#trolleyNumber").focus(); 
						  
						  this.enterEsc = function(event){
					          if(event.keyCode === 13){
					            return false; //阻止系统默认回车事件
					          }
				        }; 
				        $(document).on('keydown', this.enterEsc);	//监听键盘事件，关闭层
					  }
					});
	      	break;
	    	case 'back':
	    		window.history.go(-1);  //返回上一页
	      	break;
	    };
	});
  
	//监听行工具事件
	table.on('tool(bindingList)', function(obj){
		var data = obj.data;
		console.log(obj)
		if(obj.event === 'unbind'){
		  layer.confirm('确定解绑吗？', function(index){
		    var tId = obj.data.tId;
		    $.ajax({
		    	type:'post',
		    	url:'${pageContext.request.contextPath }/productInspection/deleteTrolleyBinding.do',
		    	data:{"tId":tId},
		    	success:function(data){
		    		if(data>0){
		    			toastr.success("解绑成功！");
		    			setTimeout(function(){  //使用  setTimeout（）方法设定定时2000毫秒
							//关闭模态框
							// 父页面刷新
							window.location.reload();  
						},2000);
		    		}else{
		    			toastr.warning("解绑失败！");
		    		}
		    	}
		    		
		    })
		    layer.close(index);
		  });
		} else if(obj.event === 'rebind'){
			var tId = obj.data.tId;
			var content1 = '<form class="layui-form layui-card-body" id="editFormId">'+
								'<table>'+
									'<tr>'+
										'<td>'+
											'<input class="layui-input" id="tId" lay-verify="tId" name="tId" type="hidden" value="'+obj.data.tId+'">'+
											'<label class="layui-form-label">台车编码 <font style="color:red; font-size: 24px;">*</font></label>'+
											'<input class="layui-input" id="trolleyNumber1" lay-verify="trolleyNumber" name="trolleyNumber" type="text" value="'+obj.data.trolleyNumber+'" style="width:300px;">'+
											'<label class="layui-form-label">产品条码 <font style="color:red; font-size: 24px;">*</font></label>'+
											'<textarea placeholder="请扫描产品条码" lay-verify="piCode" name="piCode" style="width:300px;height: 250px" class="layui-textarea">'+obj.data.piCode+'</textarea>'+
										'</td>'+
										'<td>'+
											'<label class="layui-form-label">产品编码</label>'+
											'<input class="layui-input" lay-verify="remark01" name="remark01" type="text" value="'+obj.data.remark01+'" style="width:300px;">'+
											'<label class="layui-form-label">产品名称</label><input class="layui-input" lay-verify="piName" name="piName" type="text" value="'+obj.data.piName+'" style="width:300px;">'+
											'<label class="layui-form-label">产品规格</label><input class="layui-input" lay-verify="piStandard"  name="piStandard" type="text" value="'+obj.data.piStandard+'" style="width:300px;">'+
											'<label class="layui-form-label">计量单位</label><input class="layui-input" lay-verify="piUnit"  name="piUnit" type="text" value="'+obj.data.piUnit+'" style="width:300px;">'+
											'<label class="layui-form-label">数量</label><input class="layui-input" lay-verify="piNum"  name="piNum" type="number" value="'+obj.data.piNum+'" style="width:300px;">'+
											'<input class="layui-hide" lay-verify="tStatus" name="tStatus" value="0">'+
										'</td>'+
									'</tr>'+
									'<tr>'+
										'<td>'+
											'</br></br>'+
											'<button class="layui-btn layui-btn-blue" id="editbuding" lay-submit lay-filter="editform">绑定</button>'+
											'<button type="reset" id="addReset" class="layui-btn layui-btn-primary" style="margin-left:140px">重置</button>'+
										'</td>'+
									'</tr>'+
								'</table>'+
							'</form>';
			layer.open({
				  type: 1 		//Page层类型
				  ,area: ['780px', '550px'] //宽  高
				  ,title: '修改绑定'
				  ,shade: 0.1 	//遮罩透明度
				  ,maxmin: true //允许全屏最小化
				  ,anim: 1 		//0-6的动画形式，-1不开启
				  ,content: content1
				  ,success:function(layero, index){
					  form.render();
					  
					  $("#trolleyNumber1").focus(); 
					  
					  this.enterEsc = function(event){
				          if(event.keyCode === 13){
				            return false; //阻止系统默认回车事件
				          }
			        }; 
			        $(document).on('keydown', this.enterEsc);	//监听键盘事件，关闭层
				  }
				});
		}else if(obj.event === 'inspect'){
			$("#inspect").attr("disabled",true);
			var tId = obj.data.tId;
			var piNumber = "PI"+(new Date()).getTime(); 
			var trolleyNumber = obj.data.trolleyNumber; 
			var remark01 = obj.data.remark01; 
			$.ajax({
				type : "post"
				,url : '${pageContext.request.contextPath}/productInspection/productInspection.do'
				,data : {tId:tId,piNumber:piNumber,trolleyNumber:trolleyNumber,remark01:remark01}
				,cache : false
				,async:false
				,success:function(res){
					if(res>0){
						toastr.success("报检成功！");
		    			setTimeout(function(){  //使用  setTimeout（）方法设定定时2000毫秒
							// 父页面刷新
							window.location.reload();  
						},2000);
		    		}else{
		    			toastr.warning("报检失败！");
		    		}
				}
			})
		}
	});
	
	
	/**
     * 新增表单校验
     */
    form.verify({
        //value：表单的值item：表单的DOM对象
        trolleyNumber: function(value, item){
            if(value == ''){
                return '台车不能为空';
            }
        },
        piCode: function(value, item){
            if(value == ''){
                return '产品条码不能为空';
            }
        },
       
    });
	
	
    /**
     * 通用表单提交(AJAX方式)（新增）
     */
    form.on('submit(addform)', function (data) {
    	//查询台车状态
    	
    	 $("#savebuding").attr("disabled",true);
    	
   		//获取产品条码
   		var trolleyNumber = $("#trolleyNumber").val();
   		
   		if(trolleyNumber == "" || trolleyNumber == undefined || trolleyNumber == null) {
   			toastr.warning("对不起，请扫描台车条码");
   		}else {
   			 $.ajax({
   		    	type:'post',
   		    	dataType: "json" ,
   		    	url:'${pageContext.request.contextPath}/cut/selecTrolley.do',
   		    	data:{"trolleyNumber":trolleyNumber},
   		    	success:function(data){
   		    		if(data >= 1) {
   		    			toastr.warning("对不起，此台车正在使用！");
   		    		}else{
   		    			$.ajax({
   		    				type : "post",
   		    				url : '${pageContext.request.contextPath}/productInspection/addTrolleyBinding.do',
   		    				data : $("#addFormId").serialize(),
   		    				cache : false,
   		    				async:false
   		    				}).done(
   		    					function(res) {
   		    						if (res == 1) {
   		    							toastr.success('绑定成功！');
   		    						}
   		    						setTimeout(function(){  //使用  setTimeout（）方法设定定时2000毫秒
   		    							//关闭模态框
   		    							// 父页面刷新
   		    							window.location.reload();  
   		    						},2000);
   		    					}
   		    				).fail(
   		    					function(res) {
   		    						toastr.error('绑定失败！');
   		    					}
   		    				)
   		    		}
   		    	}
   		    })
   		}
   		return false;
    });
    /**
     * 通用表单提交(AJAX方式)(修改)
     */
    form.on('submit(editform)', function (data) {
    	//查询台车状态
		$("#editbuding").attr("disabled",true);
   		//获取产品条码
   		var trolleyNumber1 = $("#trolleyNumber1").val();
   		if(trolleyNumber1 == "" || trolleyNumber1 == undefined || trolleyNumber1 == null) {
   			toastr.warning("对不起，请扫描台车条码");
   		}else {
   			 $.ajax({
   		    	type:'post',
   		    	dataType: "json" ,
   		    	url:'${pageContext.request.contextPath}/cut/selecTrolley.do',
   		    	data:{"trolleyNumber":trolleyNumber1},
   		    	success:function(data){
   		    		if(data >= 1) {
   		    			toastr.warning("对不起，此台车正在使用！");
   		    		}else{
   		    			$.ajax({
   		    				url : '${pageContext.request.contextPath}/productInspection/editTrolleyBinding.do',
   		    				data : $("#editFormId").serialize(),
   		    				cache : false,
   		    				async:false
   		    				}).done(
   		    					function(res) {
   		    						if (res == 1) {
   		    							toastr.success('修改成功！');
   		    						}
   		    						setTimeout(function(){  //使用  setTimeout（）方法设定定时2000毫秒
   		    							//关闭模态框
   		    							// 父页面刷新
   		    							window.location.reload();  
   		    						},2000);
   		    					}
   		    				).fail(
   		    					function(res) {
   		    						toastr.error('修改失败！');
   		    					}
   		    				)
   		    		}
   		    	}
   		    })
   		}
   		return false;
    });
});

toastr.options.positionClass = 'toast-top-center'; //提示框的位置设置为中上
</script>
</body>
</html>
